<?php
class BaseAction extends Action {
    static $config = null;
    function _initialize() {
        import('@.ORG.Session');
        import('@.ORG.Page');
        if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
            //重定向
            redirect(PHP_FILE .C('USER_AUTH_GATEWAY'));
        }
        $configs = D("Option")->select();

        foreach ($configs as $value) {
            self::$config[$value['type']] = $value['value'];
        }
        $this->assign('menu',  $this->menu());

        $this->assign('siteUrl',  'http://'.$_SERVER['HTTP_HOST']);
        $this->assign(self::$config);
    }
    public function _batch($modulename) {
        $checkboxid = $_REQUEST['id'];
        if(!$checkboxid) $this->error("请选择记录！");
        $act = $_REQUEST['act'];
        if(!$act) $this->error("请选择操作类型！");
        $allowact = array('remove','delete');
        if(!in_array($act,$allowact)) $this->error('未知操作');
        $id = is_array($checkboxid)?implode(',',$checkboxid):$checkboxid;
        if(!$id) $this->error('ID丢失');
        $tableId = array('Member'=>'id','Activity'=>'id','Categroy'=>'id','Message'=>'id','Pages'=>'id','Navigation'=>'id','Mails' =>'id','Link'=>'id');
        isset($modulename) ? $modulename = $modulename : $modulename = MODULE_NAME;
        switch($act) {
            case "remove":
                $Result = D($modulename)->execute('UPDATE __TABLE__ SET id ='.$_REQUEST['category'].' WHERE '.$tableId[$modulename].' IN ('.$id.')');
                $msg = "移动成功！";
                break;
            case "delete":
                if($modulename == "Categroy") {
                    $procount = D("Activity")->where($tableId[$modulename].' IN ('.$id.')')->count();
                    if($procount) $this->error("分类必需为空，请先转移或清空该分类下的活动！");
                }elseif($modulename == "Member") {
                    if(in_array(Session::get(C('USER_AUTH_KEY')),$checkboxid)||$checkboxid == Session::get(C('USER_AUTH_KEY'))) $this->error("不能删除自己的账号！");
                }
                $Result = D($modulename)->execute('DELETE FROM __TABLE__ where '.$tableId[$modulename].' IN ('.$id.')');
                $msg = "删除成功！";
                break;
            default: $this->error(L('_OPERATION_WRONG_'));
                break;
        }
        if($Result === false) {
            $this->error(L('_OPERATION_FAIL_'));
        }else {
            $this->success($msg);
        }
    }
    /**
     +------------------------------------------------------------------------------
     * 公共 文件上传方法
     +------------------------------------------------------------------------------
     * $path    string  上传路径
     * $maxsize int     上传文件最大值
     * $thumb   boolean 是否生成缩略图
     * $width   int     缩略图最大宽度
     * $height  int     缩略图最大高度
     * $autosub boolean 是否使用子目录保存文件
     +------------------------------------------------------------------------------
     */
    public function _upload($path,$thumb,$width,$height,$autosub,$maxsize) {
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        isset($maxsize) ? $upload->maxSize = $maxsize : $upload->maxSize = 1048576; //1M
        isset($path) ? $upload->savePath = $savepath = "./Attachments/".$path."/" : $upload->savePath = "./Attachments/Others/";
        if(!is_dir($savepath)) @mk_dir($savepath);
        $upload->allowExts = explode(',','gif,png,jpg,jpeg');
        if($thumb) {
            $upload->thumb = true;
            $upload->thumbPrefix = '';
            $upload->thumbSuffix = '_thumb';
            isset($wideh) ? $upload->thumbMaxWidth = $width : $upload->thumbMaxWidth = "300";
            isset($height) ? $upload->thumbMaxHeight = $height : $upload->thumbMaxHeight = "400";
        }
        if($autosub) {
            $upload->autoSub = true;
            $upload->subType = 'date';
            $upload->saveRule = time;
            $upload->dateFormat = 'Y/m/d';
        }
        if(!$upload->upload()) {
            $this->error($upload->getErrorMsg());
        }else {
            $imginfo = $upload->getUploadFileInfo();
            $imginfo = $imginfo[0]['savename'];
        }
        return $imginfo;
    }

    public  function _getCate()
    {


        if(S('cateb')) {
                    $cat_value = S('cateb');
        }else{
                    include COMMON_PATH.'tree.php';
                    $Tree = new Tree('');
                    $Categroy = D("Categroy");
                    // 把查询条件传入查询方法
                    $cat_value = $Categroy->order('fatherid asc')->select();

                    foreach ($cat_value AS $id => $row) {
                        $Tree->setNode($row['id'], $row['fatherid'], $row['name']);
                    }
                    $_cat = $Tree->getChilds();

                    foreach ($_cat as $key=>$id) {
                        $cat_value[$key]['id'] = $id;
                        $c_data = $Categroy->where('id='.$id)->find();
                        $cat_value[$key]['name'] = $Tree->getLayer($id, '|-').$Tree->getValue($id);
                        $cat_value[$key]['pinyin'] = $c_data['pinyin'];
                        $cat_value[$key]['fatherid'] = $c_data['fatherid'];
                        $cat_value[$key]['orderid'] = $c_data['orderid'];
                    }
                S('cateb',$cat_value);
        }
        return $cat_value;
    }

    public  function _getCateOption()
    {

        if(S('cateOption')) {
                    $new_cat = S('cateOption');
        }else{
            include COMMON_PATH.'tree.php';

            $Tree = new Tree('顶级分类');
            $Categroy = D("Categroy");
            $cat_value = $Categroy->order('fatherid asc')->select();

            foreach ($cat_value AS $id => $row) {
                $Tree->setNode($row['id'], $row['fatherid'], $row['name']);
            }

            $categorys = $Tree->getChilds();
			print_r($categorys);
            //遍历输出
            $new_cat = array();
            foreach ($categorys as $key=>$id) {
                $new_cat[$id] = $Tree->getLayer($id, '&nbsp;').$Tree->getValue($id);
            }

         S('cateOption',$new_cat);
        }
        return $new_cat;
    }


    //后置的操作好像不大起作用，前置的倒是可以，暂时这么用吧
    // 添加操作时清空缓存
    public function _before_doadd() {
        $Cache = Cache::getInstance();
        $Cache->clear();

    }

    // 修改操作时清空缓存
    public function _before_doedit() {
        $Cache = Cache::getInstance();
        $Cache->clear();

    }

    // 删除操作时清空缓存
    public function _before_batch() {
        $Cache = Cache::getInstance();
        $Cache->clear();

    }
    public function menu() {
        $menu = array(
            '控制面板'=>array('首页'=>'Index'),
            '活动管理'=>array('活动列表'=>'Activity','分类列表'=>'Categroy'),
            '用户管理'=>array('用户列表'=>'Member','邮件列表'=>'Mails','用户留言'=>'Message'),
            '内容管理'=>array('友情链接'=>'Link','单页分类'=>'Page','文章管理'=>'Article'),
            '系统设置'=>array('邮件设置'=>'Setting','模板设置'=>'Template','地区管理'=>'Region')

        );
        return $menu;
    }

}
